package com.hzit.controller;


import com.hzit.annotation.RequiresPermissions;
import com.hzit.dto.CategoryDTO;
import com.hzit.entity.Category;
import com.hzit.enums.Logical;
import com.hzit.service.FileService;
import com.hzit.service.ICategoryService;
import com.hzit.utils.PageResult;
import com.hzit.utils.R;
import com.hzit.vo.SearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-31
 */
@RestController
@RequestMapping("/admin/category")
@Api("广告操作")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private FileService fileServicr;


    @PostMapping("/upload")
    @ApiOperation("图片上传")
    // @RequiresPermissions(value = {},logical = Logical.AND)
    public R upload(@RequestBody @ApiParam("需要上传的文件")MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String url =fileServicr.upload(inputStream,originalFilename);
        return R.ok().data("url",url);
    }

    @PostMapping("/search/{page}/{pageSize}")
    @ApiOperation("条件查询带分页")
    public R search(@PathVariable @ApiParam(value = "页码",required = true) Integer page,
            @PathVariable @ApiParam(value = "页的大小",required = true) Integer pageSize,
            @RequestBody @ApiParam("查询条件")SearchVO searchVO){
        PageResult<CategoryDTO> pageResult=categoryService.search(page,pageSize,searchVO);

       return R.ok().data("list",pageResult);
    }

    @GetMapping("/remove/{ids}")
    @ApiOperation("删除广告")
    public R remove(@PathVariable @ApiParam("需要删除的广高id") String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        categoryService.removeBatchByIds(list);
        return R.ok().message("删除成功");
    }

    @PostMapping("/update")
    @ApiOperation("修改广告信息")
    public R update(@RequestBody @ApiParam("修改广告的信息 带id") Category category){
        categoryService.updateById(category);
        return R.ok().message("修改成功");
    }

    @PostMapping("/add")
    @ApiOperation("添加广告")
    public R add(@RequestBody @ApiParam("添加广告，不带id") Category category){
        categoryService.save(category);
        return R.ok().message("添加成功");
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("利用id查询广告")
    public R findById(@PathVariable Integer id){
        Category byId = categoryService.getById(id);
        return R.ok().data("category",byId);
    }
}
